<?php
namespace Api\Controller;
use Think\Controller;
class DeliveryController extends Controller {
    /**
	 * 24小时快递api
	 * @param string  
	 * @return   
	 */
	public function index(){
        // $this->display('login');
        $this->show('24小时快递-国内首家先行赔付快递服务商(api/delivery)');
        $this->display('');
    }
	/**
	 *验证token 	测试地址 http://www.boss24delivery.com/index.php/api/user/checkUserToken
	 * @param string 用户名post.phone
	 * @param string token值post.token 	
	 * @return  json
	 */ 
	public function checkUserToken($phone,$token){ 
				$member					= D('member');	 
				$map					= array();	
				$map['status']			= 1;	
				$map['is_del']			= 0;
				$map['phone']			= $phone;
				$res					= $member->where($map)->find();  
				//获取密码和sloat值
				$pwd					= $res['pwd'];
				$sloat					= $res['sloat']; 
				// 拼接token
				$tokenV					= md5('@'.$phone.'#'.md5($pwd.'$'.$sloat.date('y-m-d')));
				if($token==$tokenV){
					return $res;
				}else{
					return false;
					// return $tokenV	;
				}  
    } 
	 
	/**
	 *拉取用户全部寄件记录 	测试地址 http://www.boss24delivery.com/index.php/api/Delivery/personalDeliveryList
	 * @param string 用户名post.phone
	 * @param string 密码post.pwd
	 * @param string 记住密码post.rememberMe	
	 * @return  json
	 */ 
	public function personalDeliveryList(){
		if(IS_POST){
				$phone					= I('post.phone');
				$token					= I('post.token');
				// token值 13269706193
				//$token='a05392a1c65bb072642a5e088e02c899';
				//$member					= D('member');	
				//token验证 返回该条记录 personInfo
				$tokenV					= $this->checkUserToken($phone,$token);   
				if(!$token){
					$data 		= array(
									'status'		=> 400,
									'info' 			=> 'token验证失败！',
									//'personInfo'			=>  $tokenV,
									//'callback' 		=> U('Index/dispalyIndex')
									); 
					$this->ajaxReturn($data);exit();
				}
				// 用户id
				$user_id			= $tokenV['id'];
				$order				= D('order');	
				$map				= array();	
				$map['client_id']	= $user_id;	
				$map['order_status']= 1;	
				$map['is_del']		= 0;	
				$res				= $order->where($map)->select();
				if($res){
					$data 		= array(
									'status'		=> 200,
									'info' 			=> '用户全部寄件记录拉取成功！',
									'personInfo'			=>  $tokenV,
									'personDelivery'			=>  $res,
									//'callback' 		=> U('Index/dispalyIndex')
									); 
					$this->ajaxReturn($data);
				}else{
					$data 		= array(
									'status'		=> 201,
									'info' 			=> '暂无该用户寄件记录！',
									//'personInfo'			=>  $tokenV,
									//'personDelivery'			=>  $res,
									//'callback' 		=> U('Index/dispalyIndex')
									); 
					$this->ajaxReturn($data);
				}	
									
		}else{
				$data 		= array(
									'status'		=> 400,
									'info' 			=> 'bad request!',
									'test'			=> '',
									'callback' 		=> ''
								);
				$this->ajaxReturn($data);
		}
		
    }
	/**
	 *拉取已签收记录（快递显示已签收） 	测试地址 http://www.boss24delivery.com/index.php/api/Delivery/personalDeliveriedList
	 * @param string 用户名post.phone
	 * @param string 密码post.pwd
	 * @param string 记住密码post.rememberMe	
	 * @return  json
	 */ 
	public function personalDeliveriedList(){
		if(IS_POST){
				$phone					= I('post.phone');
				$token					= I('post.token');
				// token值 13269706193
				//$token='a05392a1c65bb072642a5e088e02c899';
				//$member					= D('member');	
				//token验证 返回该条记录 personInfo
				$tokenV					= $this->checkUserToken($phone,$token);   
				if(!$token){
					$data 		= array(
									'status'		=> 400,
									'info' 			=> 'token验证失败！',
									//'personInfo'			=>  $tokenV,
									//'callback' 		=> U('Index/dispalyIndex')
									); 
					$this->ajaxReturn($data);exit();
				}
				// 用户id
				$user_id			= $tokenV['id'];
				$order				= D('order');	
				$map				= array();	
				$map['client_id']	= $user_id;
				$map['order_status']		= 1;	
				$map['is_del']				= 0;
				//快递已签收	
				$map['delivery_status']		= 2;	
				$res				= $order->where($map)->find();
				if($res){
					$data 		= array(
									'status'		=> 200,
									'info' 			=> '用户全部已签收记录拉取成功！',
									'personInfo'			=>  $tokenV,
									'personDelivery'			=>  $res,
									//'callback' 		=> U('Index/dispalyIndex')
									); 
					$this->ajaxReturn($data);
				}else{
					$data 		= array(
									'status'		=> 201,
									'info' 			=> '暂无已签收记录！',
									//'personInfo'			=>  $tokenV,
									//'personDelivery'			=>  $res,
									//'callback' 		=> U('Index/dispalyIndex')
									); 
					$this->ajaxReturn($data);
				}	
									
		}else{
				$data 		= array(
									'status'		=> 400,
									'info' 			=> 'bad request!',
									'test'			=> '',
									'callback' 		=> ''
								);
				$this->ajaxReturn($data);
		}
		
    }
	/**
	 *用户名 	测试地址 http://www.boss24delivery.com/index.php/api/user/checkUserExists
	 * @param string 用户名post.phone  手机号码
	 * @param string 密码post.pwd
	 * @param string 记住密码post.rememberMe	
	 * @return  json  132--0123
	 */ 
	public function checkUserExists(){
		if(IS_POST){
				$phone					= I('post.phone');
				$member					= D('member');	
				//验证用户名 
				$map					= array();	
				$map['status']			= 1;	
				$map['is_del']			= 0;
				$map['phone']			= $phone;  
				$res_user				= $member->where($map)->find();
				//手机号码已经存在
				if($res_user){
									$data 		= array(
									'status'		=> 201,
									'info' 			=> '该手机已经被注册,请直接登录！如忘记密码可直接用验证码登录',
									 );
				}else {
						//手机号未被注册
									$data 		= array(
									'status'		=> 200,
									'info' 			=> '手机号可以注册！',
									 );
						}  
				$this->ajaxReturn($data);
		}else{
				$data 		= array(
									'status'		=> 400,
									'info' 			=> 'bad request!',
									'test'			=> '',
									'callback' 		=> ''
								);
				$this->ajaxReturn($data);
		}
		
    }
	public function generateRandomString($length=6){
		$characters='0123456789';
		$randomString='';
		for($i=0;$i<$length;$i++){
			$randomString.=$characters[rand(0,strlen($characters)-1)];
			}
			return$randomString;
	}			
	  
}